package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.domain.Permission;
import com.woniuxy.domain.PermissionRole;
import com.woniuxy.dto.DtoJSONResult;
import com.woniuxy.dto.DtoJSONResultCode;
import com.woniuxy.dto.DtoPermission;
import com.woniuxy.dto.DtoRolePermissionResponse;
import com.woniuxy.exception.RolePermissionException;
import com.woniuxy.parameter.UpdatePermissionRoleParam;
import com.woniuxy.service.PermissionRoleService;
import com.woniuxy.service.PermissionService;
import com.woniuxy.shiro.JWTUtils;
import com.woniuxy.util.BeanCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PermissionController
 * @Description 权限控制器
 * @Author Joker
 * @Version 1.0.0
 **/
@RestController
@RequestMapping("/permission")
@CrossOrigin
@Api(tags = "权限分类控制器")
public class PermissionController {
    @Resource
    private PermissionService permissionService;
    @Resource
    private PermissionRoleService permissionRoleService;
    /**
     * @Description 通过账号找到该角色权限
     * @Author Joker
     * @Date 15:28 2021/5/13
     * @Param [account] user表中的account
     * @Return com.woniuxy.dto.JSONResult
     */
    @ApiOperation("根据账号找到该账号的权限")
    @GetMapping("/getPermissionById")
//    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult selectPermissionByAccount(HttpServletRequest request) throws Exception{
        String token = request.getHeader("token");
        int id = Integer.parseInt(JWTUtils.getId(token));
        List<Permission> permissions = permissionService.selectPermissionByUserId(id);
        List<DtoPermission> dtoPermissions =(ArrayList) BeanCopyUtil.copyListProperties(permissions, DtoPermission::new);
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(),DtoJSONResultCode.SUCCESS.getMessage(),null,dtoPermissions);
    }
    /**
     *@Author 王昊波
     *@Description 根据角色id查找到该角色的权限
     *@Param [roleId]
     *@return com.woniuxy.dto.DtoJSONResult
     */
    @ApiOperation("根据角色id查找到该角色的权限")
    @GetMapping("/getPermissionByRole")
    @RequiresPermissions({"角色权限管理"})
    public DtoJSONResult selectPermissionByRole(int roleId) throws Exception{
        QueryWrapper<PermissionRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<PermissionRole> permissionRoles = permissionRoleService.list(wrapper);
        List<DtoPermission> dtoPermissions = BeanCopyUtil.copyListProperties(permissionRoles, DtoPermission::new);
        int[] ids = new int[dtoPermissions.size()];
        for (int i = 0; i < dtoPermissions.size(); i++) {
            ids[i] = dtoPermissions.get(i).getPermissionId();
        }
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(),DtoJSONResultCode.SUCCESS.getMessage(),ids,null);
    }
    /**
     *@Author 王昊波
     *@Description 查询所有权限
     *@Param []
     *@return com.woniuxy.dto.DtoJSONResult
     */
    @ApiOperation("查询所有的权限")
    @GetMapping("/getAll")
    @RequiresPermissions({"角色权限管理"})
    public DtoJSONResult selectAllPermission() throws Exception{
        List<Permission> permissions = permissionService.selectAllPermission();
        List<DtoPermission> dtoPermissions = BeanCopyUtil.copyListProperties(permissions, DtoPermission::new);
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(),DtoJSONResultCode.SUCCESS.getMessage(),null,dtoPermissions);
    }
    /**
     *@Author 王昊波
     *@Description 根据角色id更新相应的所有权限
     *@Param [roleId]
     *@return com.woniuxy.dto.DtoJSONResult
     */
    @PostMapping("/update")
    @ApiOperation("根据角色id和权限id更新对应角色的权限")
    @RequiresPermissions({"角色权限管理"})
    public DtoJSONResult updatePermissionRole(UpdatePermissionRoleParam param) throws Exception{
        QueryWrapper<PermissionRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",param.getRoleId());
        permissionRoleService.remove(wrapper);
        List<Integer> list = param.getList();
        for (int i = 0; i < list.size(); i++) {
            PermissionRole permissionRole = new PermissionRole();
            permissionRole.setRoleId(param.getRoleId());
            permissionRole.setPermissionId(list.get(i));
            permissionRoleService.save(permissionRole);
        }
        return new DtoJSONResult(DtoRolePermissionResponse.UPDATEROLEPERMISSIONSUCCESS.getCode(), DtoRolePermissionResponse.UPDATEROLEPERMISSIONSUCCESS.getMessage(), null,null);
    }
}

